﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using DMS.Common.Model.Result;
using SqlSugar.Template.IService;
using SqlSugar.Template.IService.Param;
using SqlSugar.Template.IService.Result;
using SqlSugar.Template.Models;

namespace SqlSugar.Template.Service
{
    public class TokenService : BaseService<UseToken>, ITokenService
    {
        public async Task<ResponseResult> Add(AddToken param)
        {
            ResponseResult result = new();
            if (param == null || param.UseId < 0)
            {
                result.errno = 1;
                result.errmsg = "参数错误";
                return result;
            }
            UseToken useToken = new()
            {
                UseId = param.UseId,
             Token = param.Token,
            };
            //插入返回自增列
            var flag = await base.InsertReturnIdentityAsync(useToken);
            //null 列不插入
            //flag = await base.AsInsertable(stock).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            result.data = flag;
            return result;

        }

        public async Task<ResponseResult> DeleteAsync(long useId)
        {
            ResponseResult result = new();
            if (useId <= 0)
            {
                result.errno = 1;
                result.errmsg = "参数不合法";
                return result;
            }

            var t1 = await Context.Deleteable<UseToken>()
                .Where(a => a.UseId == useId)
                .ExecuteCommandAsync();
            result.data = t1;
            return result;
        }

        public async Task<ResponseResult<UseToken>> GetTokenAsync(long useId)
        {
            ResponseResult<UseToken> result = new() { data = new UseToken() };
            var entity = await Context.Queryable<UseToken>()
                .Select<UseToken>()
                .FirstAsync(q => q.UseId == useId);
            if (entity == null)
            {
                result.errno = 1;
                result.errmsg = "未找到相关数据";
                return result;
            }
            result.data = entity;
            return result;
        }

      
    }
}
